package oocis.oocisSharing;

import java.io.IOException;
import java.util.*;
import oocis.util.*;

public class Controller {

	private static String username = "guest";

	private static String password = "123456";

	private static String svnaddress = "svn://localhost/repos2/";

	private static String svnpath = "D:\\oocisWC\\";

	private static int ftpport = 21;

	public Controller() {
		username = "guest";

		password = "123456";

		svnaddress = "svn://localhost/repos2/";

		svnpath = "D:\\oocisWC\\";

		ftpport = 21;

	}

	public Controller(String _username, String _password, String _svnaddress,
			String _svnpath, int _ftpport) {
		username = _username;
		password = _password;
		svnaddress = _svnaddress;
		svnpath = _svnpath;
		ftpport = _ftpport;
	}

	/**
	 * uploading a file
	 * 
	 * @param _serverName
	 * @param _localFile
	 * @param _remoteFile
	 * @param message
	 * @return -1 : ftp uploading error 0 : success 1 : exist!
	 * @throws IOException
	 */
	public int upload(String _serverName, String _localFile,
			String _remoteFile, String message, boolean flag)
			throws IOException {
		Server srv = new Server(_serverName, svnaddress, ftpport);
		FTPClient fc = new FTPClient(_serverName, GetIP.getIP(), ftpport,
				username, password);
		fc.connectServer();
		if (fc.upload(_localFile, _remoteFile) == false) {
			fc.closeConnect();
			ExtCall.exec("cmd /c del " + svnpath + _remoteFile);
			return -1;
		}
		fc.closeConnect();
		SourceSearching ss = new SourceSearching(srv, "", _remoteFile);
		int ssr = ss.operate();
		System.out.println(ssr+"!!!!!!!!!");
		if (ssr == 0 && flag == false) {
			ExtCall.exec("cmd /c del " + svnpath + _remoteFile);
			return 1;
		} else {
			SourceUploading su = new SourceUploading(srv, svnpath, _remoteFile,
					"\"" + message + "\"");
			int t = su.operate();
			ExtCall.exec("cmd /c del " + svnpath + _remoteFile);
			return t;
		}

	}

	/**
	 * 
	 * @param _serverName
	 * @param _localFile
	 * @param _remoteFile
	 * @param message
	 * @return -1: ftp downloading error 0:success;
	 * @throws IOException
	 */

	public int download(String _serverName, String _localFile,
			String _remoteFile, String message) throws IOException {
		Server srv = new Server(_serverName, svnaddress, ftpport);
		SourceDownloading a = new SourceDownloading(srv, "", _remoteFile,
				svnpath + _remoteFile);
		int t = a.operate();
		if (t != 0)
			return t;
		FTPClient fc = new FTPClient(_serverName, GetIP.getIP(), ftpport,
				username, password);
		fc.connectServer();
		if (fc.download("", _remoteFile, _localFile) == false) {
			return -1;
		}
		fc.closeConnect();
		ExtCall.exec("cmd /c del " + svnpath + _remoteFile);
		return 0;
	}

	public static ArrayList<String> getList(String _serverName)
			throws IOException {
		Server srv = new Server(_serverName, svnaddress, ftpport);
		SourceListing sl = new SourceListing(srv, "");
		return sl.listing("");
	}

	public static void main(String[] argvs) throws IOException {
		Controller c = new Controller();
		int t = c
				.upload(
						"server1",
						"C:\\Documents and Settings\\BokChow\\Desktop\\oo\\org.tmatesoft.svn_1.1.4.eclipse.zip",
						"org.tmatesoft.svn_1.1.4.ecc.zip", "a1", false);
		System.out.println(t);

		ArrayList<String> a = c.getList("server1");
		Iterator<String> iter = a.iterator();
		String l;

		System.out.println("These are the laksjdflasjdlf");
		while (iter.hasNext()) {
			l = iter.next();
			System.out.println(l);
		}

		System.out.println("alksdjflasjdflaj");
		c.download("server1", "D:\\test1.rar", "test.rar", "a11");
	}
}
